//
//  LKIntroductionViewController.m
//  LKIntroductionPage
//
//  Created by rimi on 15/10/9.
//  Copyright (c) 2015年 Liukai. All rights reserved.
//

#import "LKIntroductionViewController.h"

@interface LKIntroductionViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *bkViews; //背景图片数组
@property (nonatomic, strong) NSArray *scrollViewPages;//覆盖图片数组
@property (nonatomic, strong) UIPageControl *pageControl;//分页控制器
@property (nonatomic, assign) NSInteger centerPageIndex;
@end

@implementation LKIntroductionViewController

//实现供外部调用的方法,使本类中的数组得到传进的值
- (id)initWithCoverImageNames:(NSArray *)coverImageNames bkImageNames:(NSArray *)bkImageName {
    self = [super init];
    if (self) {
        self.coverImageName = coverImageNames;
        self.bkImageNames = bkImageName;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackGroundViews];
    //scrollView的创建
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    //pageControl的创建
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 30, self.view.bounds.size.width, 30)];
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:self.pageControl];
    //创建enterButton
    if(!self.enterButton) {
        self.enterButton = [UIButton new];
        [self.enterButton setTitle:@"进入体验" forState:UIControlStateNormal];
        self.enterButton.layer.borderColor = [UIColor whiteColor].CGColor;
        self.enterButton.layer.borderWidth = 0.5;
    }
    [self.enterButton addTarget:self action:@selector(handleEvent:) forControlEvents:UIControlEventTouchUpInside];
            self.enterButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds) / 2 - 50,CGRectGetHeight(self.view.bounds) -60,100,50);
            self.enterButton.alpha = 0;
    [self.view addSubview:self.enterButton];
    [self reloadPages];
}


//为数组添加背景图片
- (void)addBackGroundViews {
    NSMutableArray *tempArray = [NSMutableArray array];
    [self.bkImageNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        //枚举背景图名字数组，添加。
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:obj]];
        imageView.frame = self.view.bounds;
        [tempArray addObject:imageView];
        [self.view addSubview:imageView];
    }];
    self.bkViews = [[tempArray reverseObjectEnumerator] allObjects];
}

//加载所有背景图
- (void)reloadPages {
    self.pageControl.numberOfPages = self.coverImageName.count;
    UIView *aView = self.scrollViewPages.firstObject;
    self.scrollView.contentSize = CGSizeMake(aView.frame.size.width * self.scrollViewPages.count, aView.frame.size.height);
    __block CGFloat x = 0;//此时scrollViewPages已有值
    [self.scrollViewPages enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
        obj.frame = CGRectOffset(obj.frame, x, 0);
        [self.scrollView addSubview:obj];
        x += obj.frame.size.width;
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //获取是第几张图片
    NSInteger index = scrollView.contentOffset.x / self.view.frame.size.width;
    CGFloat alpha = 1 - (scrollView.contentOffset.x - index * self.view.frame.size.width) / self.view.frame.size.width;
    if (self.bkViews.count > index) {
        UIView *bkView = self.bkViews[index];
        if (bkView) {
            [bkView setAlpha:alpha];
        }
    }
    //currentpage当前页。
    self.pageControl.currentPage = scrollView.contentOffset.x /CGRectGetWidth(self.view.bounds);
    [self coverScrollViewDidChangePages:scrollView];
}

- (void)coverScrollViewDidChangePages:(UIScrollView *)scrollView {
    if ([self isLast:self.pageControl]) {
        self.pageControl.alpha = 1;
        self.enterButton.alpha = 0;
        [UIView animateWithDuration:0.4 animations:^{
            self.pageControl.alpha = 0;
            self.enterButton.alpha = 1;
        }];
    }
    else {
        if (self.pageControl.alpha == 0) {
            [UIView animateWithDuration:0.4 animations:^{
                self.enterButton.alpha = 0;
                self.pageControl.alpha = 1;
            }];
        }
    }
}

- (BOOL)isLast:(UIPageControl *)pageControl {
    return pageControl.numberOfPages == pageControl.currentPage + 1;
}

#pragma mark -- Getter 插语背景图
- (NSArray *)scrollViewPages {
    if (self.coverImageName.count == 0) {
        return nil;
    }
    if (_scrollViewPages) {
        return _scrollViewPages;
    }
    NSMutableArray *tempArray = [NSMutableArray new];
    [self.coverImageName enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIImageView *coverImageView = [self scrollViewPage:obj];
        [tempArray addObject:coverImageView];
    }];
    _scrollViewPages = tempArray;
    return _scrollViewPages;
    
}

//传进一个名字，返回一个设置过图片的coverImageView
- (UIImageView *)scrollViewPage:(NSString *)imageName {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
      CGSize size = {[[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height};
    imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, size.width, size.height);
    return imageView;
}

//按钮处理事件
- (void)handleEvent:(UIButton *)sender {
    if (self.didSelectedEnter) {
        self.didSelectedEnter();
    }
}












@end
